from main import *
from threading import *

Main.init()

class Client:
    left_on = False
    right_on = False
    w = False
    s = False
    a = False
    d = False

    @EventBus.on(KeyPressedEvent)
    def pressevent(event):
        if event.key == pygame.K_ESCAPE:
            Main.stop()
        elif event.key == pygame.K_LEFT:
            Client.left_on = True
        elif event.key == pygame.K_RIGHT:
            Client.right_on = True
        elif event.key == pygame.K_w:
            Client.w == True
        elif event.key == pygame.K_s:
            Client.s == True
        elif event.key == pygame.K_a:
            Client.a == True
        elif event.key == pygame.K_d:
            Client.d == True

    @EventBus.on(KeyReleasedEvent)
    def releaseevent(event):
        if event.key == pygame.K_LEFT:
            Client.left_on = False
        elif event.key == pygame.K_RIGHT:
            Client.right_on = False
        elif event.key == pygame.K_w:
            Client.w == False
        elif event.key == pygame.K_s:
            Client.s == False
        elif event.key == pygame.K_a:
            Client.a == False
        elif event.key == pygame.K_d:
            Client.d == False
    
    @EventBus.on(TickEvent)
    def tick(event):
        if Client.left_on:
            Main.player.obb.rotate(-10)
        elif Client.right_on:
            Main.player.obb.rotate(10)
        elif Client.w:
            Main.player.obb.move((-10, 0))
        elif Client.s:
            Main.player.obb.move((10, 0))
        elif Client.a:
            Main.player.obb.move((0, -10))
        elif Client.d:
            Main.player.obb.move((0, 10))

if __name__ == '__main__':
    render_thread = Thread(target=Main.render)
    render_thread.start()

    Main.main()